import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author SK
 * @date 2022/3/19-9:49
 */
public class Test {
    public static boolean flag=false;
    public static void main(String[] args) {
        menu();
    }

    public static void menu(){
        Scanner scanner=new Scanner(System.in);
        Timer timer=new Timer();
        Task task=new Task();
        timer.schedule(task,0,5000);
        while (true){
            System.out.println("1. 定时开始");
            System.out.println("2. 定时停止");
            System.out.println("0. 退出");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    flag=true;
                    break;
                case 2:
                    flag=false;
                    break;
                case 0:
                    timer.cancel();
                    return;
                default:
                    System.out.println("请输入正确指令");
                    break;
            }
        }
    }

    static class Task extends TimerTask{

        @Override
        public void run() {
            if (flag){
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH:mm:ss");
                Date date=new Date();
                String simpleDate = simpleDateFormat.format(date);
                System.out.println(simpleDate);
            }
        }
    }
}
